package service;


import mapper.BrandMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class BrandService {

    public List<Brand> selectAll(){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//        1开启会话
        SqlSession sqlSession = factory.openSession();
 //        2获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//      3执行查询
        List<Brand> brands = mapper.selectAll();
//        4.关闭sql
        sqlSession.close();
//        5.返回数据
        return brands;
    }
//     添加品牌
    public void add(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        brandMapper.add(brand);
//提交事务
        sqlSession.commit();
//释放资源
        sqlSession.close();
    }

//    查询单个
    public Brand selectById(int id){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//        1开启会话
        SqlSession sqlSession = factory.openSession();
        //        2获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//      3执行查询
        Brand brand = mapper.selectById(id);
//        4.关闭sql
        sqlSession.close();
//        5.返回数据
        return brand;
    }

    //品牌修改
public static void update(Brand brand) {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    // 开启会话
    SqlSession sqlSession = factory.openSession();
    // 获取Mapper
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //3执行修改
    brandMapper.update(brand);
    sqlSession.commit();
    //5.关闭sqlSession
    sqlSession.close();
    }
//删除一个品牌
    public void delete(int id){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行修改
        brandMapper.deleteById(id);

        sqlSession.commit();
        //5.关闭sqlSession
        sqlSession.close();
    }

//品牌分页查询
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize){
//        1开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //        2获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//      3执行查询
        PageBean<Brand> pageBean = new PageBean<>();
//        3.1查询某一页数据
        int beginIndex = (currentPage - 1)*pageSize;//偏移量
        List<Brand> brandList = mapper.selectAllPage(beginIndex, pageSize);
//        3.2
        int totalCount = mapper.selectCount();
//        3.3封装分页数据为PageBen
        pageBean.setData(brandList);
        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        //计算总页数
        int totalPage = (int)(Math.ceil(totalCount*1.0/pageSize)) ;
        pageBean.setTotalPage(totalPage);
//        4.关闭sql
        sqlSession.close();
//        5.返回数据
        return pageBean;
    }


    //分页条件查询

    //分页条件查询
    public static List<Brand> states(int status,String brandName){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行查询
        List<Brand> SelectStatusAllServlet = brandMapper.SelectStatusAllServlet(status, brandName);

        sqlSession.commit();
        //5.关闭sqlSession
        sqlSession.close();
        return SelectStatusAllServlet;
    }


    //批量删除
    public void deleteByIds(int[] ids){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
        mapper.deleteByIds(ids);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }
}